/**
 * jims
 */
package com.jims.master.modules.base.user.service;


import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;

import com.jims.master.modules.base.user.dao.SysDepartmentDao;
import com.jims.master.modules.base.user.entity.MasterDoctorinfo;
import com.jims.master.modules.base.user.entity.SysDepartment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 科室表Service
 * @author xueyx
 * @version 2017-06-01
 */
@Service
@Transactional(readOnly = true)
public class SysDepartmentService extends CrudService<SysDepartmentDao, SysDepartment> {

	@Autowired
	private MasterDoctorinfoService masterDoctorinfoService;


	@Override
    public SysDepartment get(String id) {
		return super.get(id);
	}
	
	@Override
    public List<SysDepartment> findList(SysDepartment sysDepartment) {
		return super.findList(sysDepartment);
	}
	
	@Override
    public Page<SysDepartment> findPage(Page<SysDepartment> page, SysDepartment sysDepartment) {
		return super.findPage(page, sysDepartment);
	}
	
	@Override
    @Transactional(readOnly = false)
	public int save(SysDepartment sysDepartment) {
		return super.save(sysDepartment);
	}
	
	@Override
    @Transactional(readOnly = false)
	public int delete(SysDepartment sysDepartment) {
		return super.delete(sysDepartment);
	}

	public List<SysDepartment> getByParentId(String parentId){
		return dao.getByParentId(parentId);
	};

	public List<SysDepartment> getSysDepartment(){
		return dao.getSysDepartment();
	};

	/**
	 * 医院科室二级联动
	 * @param mechanism
	 * @author 陈涛
	 * @return
	 */
	public List<SysDepartment> partsList(String mechanism){
		List<SysDepartment> partsList = new ArrayList();
		SysDepartment sysDepartment;
		SysDepartment sysDepartment1;
		//查询注册医院所有医生父级科室
		List<MasterDoctorinfo> hospParts = masterDoctorinfoService.getPartsByMechanism(mechanism);
		for (int i = 0; i < hospParts.size(); i++){
			String id = hospParts.get(i).getParts();
			sysDepartment = this.get(id);
			if(sysDepartment!=null){
				if (sysDepartment.getParent().getId().equals("0")){
				}else{
					sysDepartment = dao.getByParent(sysDepartment.getParent().getId());
				}
				partsList.add(sysDepartment);
			}
		}
		//去掉重复父级科室
		partsList = removeDuplicateUser(partsList);
		//得到注册医生的子科室并放进父级科室中
		for (int i = 0; i < partsList.size(); i++){
			List<SysDepartment> partsList1 = new ArrayList();
			for (int j = 0; j < hospParts.size(); j++){
				String id = hospParts.get(j).getParts();
				sysDepartment1 = this.get(id);
				if(sysDepartment1!=null){
					if (partsList.get(i).getId().equals(sysDepartment1.getParent().getId())){
						partsList1.add(sysDepartment1);
					}
				}
			}
			//去掉重复子科室
			partsList1 = removeDuplicateUser(partsList1);
			partsList.get(i).setSysDepartmentList(partsList1);
		}
		return partsList ;
	}

	private static ArrayList<SysDepartment> removeDuplicateUser(List<SysDepartment> sysDepartment) {
		Set<SysDepartment> set = new TreeSet<SysDepartment>(	new Comparator<SysDepartment>() {
			@Override
			public int compare(SysDepartment o1, SysDepartment o2) {
				//字符串,则按照asicc码升序排列
				return o1.getId().compareTo(o2.getId());
			}
		});
		set.addAll(sysDepartment);
		return new ArrayList<SysDepartment>(set);
	}
	public List<SysDepartment> getParentPart() {
		return dao.getParentPart();
	}

	/**
	 *
	 * @return
	 */
	public String getDepNo(String parts) {
		return dao.getDepNo(parts);
	}

}